package fr.afcepf.atod18.controler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.afcepf.atod18.action.ActionIndex;
import fr.afcepf.atod18.action.ActionLogin;
import fr.afcepf.atod18.action.ActionLogout;
import fr.afcepf.atod18.action.InterfaceAction;
import fr.afcepf.atod18.factory.Factory;
import fr.afcepf.atod18.form.ActionForm;
import fr.afcepf.atod18.form.IndexForm;
import fr.afcepf.atod18.form.LoginForm;
import fr.afcepf.atod18.form.LogoutForm;

/**
 * Servlet implementation class ServletDispatch
 * Cette servlet constitue le cont�leur
 */
public class ActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	Map<String, Class<?>> mapAction  = new HashMap<String, Class<?>>();
	Map<String, Class<?>> mapForm  = new HashMap<String, Class<?>>();

	Factory factory;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActionServlet() {
        super();
    }
    
    
	public void init(ServletConfig config) throws ServletException {
		
		super.init(config);
		
		mapAction.put("/index.html", ActionIndex.class);
		mapAction.put("/ActionLogout.do", ActionLogout.class);
		mapAction.put("/ActionLogin.do", ActionLogin.class);

		mapForm.put("/index.html", IndexForm.class);
		mapForm.put("/ActionLogout.do", LogoutForm.class);
		mapForm.put("/ActionLogin.do", LoginForm.class);

		factory  = new Factory();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String provenance = request.getServletPath();
		
		// affichage des saisies de la form
		Map<String, String[]> mapParam = request.getParameterMap();
		Class<?> formClass = mapForm.get(request.getServletPath());
		
        ActionForm formAction = null;
		try {
			formAction = (ActionForm) formClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
		MyBeanPopulate.populateBean(formAction, mapParam);
		
		InterfaceAction a = factory.done(provenance);
	
		String suite = a.execute(request, response);
		String actionSuite = suite.substring(0,1) + "Action" + suite.substring(1,2).toUpperCase()
				+ suite.substring(2,suite.length()-3) + "do";
//		System.out.println("provenance=" + provenance + " suite=" + suite + " actionSuite=" + actionSuite);
		// recharger qd même écran
		if (actionSuite.equals(provenance)) {
			request.setAttribute("champForm", formAction);
		}
		request.getRequestDispatcher(suite).forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet( request, response);
	}

}
